<?php
/**
 * Created by PhpStorm.
 * User: lcz
 * Date: 2018/8/8
 * Time: 9:58
 * 测试控制器
 */
namespace app\index\controller;

use app\common\controller\Weixin;
use app\common\model\AgentArea;
use app\common\model\Pdd;
use app\common\model\WeixinOpen;
use think\Cache;
use think\Controller;
use think\Db;
use think\Log;

class Test extends Controller{
	
	public function getToken(){
		$obj = new Weixin();
		$res = $obj->getAccessToken();
		dump($res);
	}
	
	public function sendTemplate(){
		$obj = new Weixin();
		
		$data = [
			'keyword1' => ['value' => '单号'],
			'keyword2' => ['value' => '金额'],
			'keyword3' => ['value' => '下单时间'],
			'keyword4' => ['value' => '物品名称'],
		];
		$res = $obj->sendTemplate('oqQ_r4hsjHjDU2RPTqml6a7lvu0A', '8wpcSa7Iu7Nv1YITc2fE0Yqdye_aIFCgekOkcLRISb0','wx081029230997560c2982824b3473633630', $data,'pages/order/detail/index?order_id=1650','keyword1.DATA');
		dump($res);
	}
	
	public function getUnionID(){
		$encryptedData = "8Tg35ebsPBwD68FsH/i4cQE+EEEStM3zI6MfSXwaF2uPluGZGue43P2ZnvUpm+AqWSoSpXKU1fB8Z0Y911mEOPpCzDnHSUtzU2GJyBkiJl1y3YOfXkx1PmJJetB2ua0+QiFggXqMLVnLYdS0PsSa8V6KMREZxgCmFzXd9aCTyPrpQ6CLyYEYHctcwZfb/o+qEXeH16EZXotLzAR/h1ktTJecJZqMEYFWy4pw9WA078S+pgu0BKqE/fefKTFNcJKHNZf0bPWlkVBG1Nyuc62OosF4Cxgtp069ve/GPG9z5K9Tf720RDxwEhxUSzApHkyB0w0eR5TA+TkMdnKXXnttUvLFalgqzQf4OcozLY98cFVqNGcWMzTv0Q3WqcyRTJmZkC8BvHJwJSKQHDGIxYmrYyUeqBC1df7p9PdaQwjniPCk5rwmlgxxeeg1wtH3A+M8bnGLudwFs0LhzBiQy5KwAQ==";
		$iv = "5cQ1HmUMSkUELr5AgdWYBA==";
	
	}
	
	
	public function weixinOpen(){
		$model = new WeixinOpen();
		//解密
//		$res = $model->decryptMsg('7fcd9f5be3e8bf0dfa8351cd0c4da77a2714630c', '1537168129', '1592920601', 'rp+erQvQt9AmzUEsDQrtlGud3YPedLWM405NENtn7hld4fs2Sy7zxpSotmK8aSxHOT+LgMxTNACm9AZlnZXZU60U9W+l0rlN9HBjaZcODcTIan75GopsczMXvstzgRGZd934ATKfGtTB1IbTJ/hOFYcKCRfnHY26tqABv4B++TtcLKuOx4zZQppqRKxo8ovY6S+oJEXlz6bSZeDZgtRqMDJGzIqmr85MKoHEo9NB+bwxsrBHMgKh0lzm+kAaK6wvPpcOY5WPSn2b55VRIU8PGqVmYatZyYUdhX9yS/2SkSb/JlWJLmK7e1MTj1JLB7w0t5BWT3vAts5uV5al4ti9YyAYk6T2mzQd1Eqq/3ETlTjDuYOz/wMbP8VSLM7VSBeZYYrPCgMWWbsgIFzyCjwRt5OFWIDQKQ7IHxXC5zlA7Ou9v72eT7cl9huuffOL4Nf5610wS/t2+lVL2wx7FQ1Eqw==');
		
		//获取token
//		$res = $model->getComponentAccessToken();
		
		//获取预授权码
		$res = $model->getPreAuthCode();
		
		//去授权
//		$url = $this->request->domain().url('api/wxcomponent/authcallback');
//		$res = $model->createAuthUrl($url);
//		echo '<a href="'.$res.'">去授权</a>';
		
		//获取授权token内容
//		$res = $model->getAuthorizationInfo();
		
		//获取apiToken
//		$res = $model->getApiAuthorizerToken();
		
		//获取授权应用信息
//		$res = $model->getAuthorizerInfo();
		dump($res);
	}
	
	public function pdd() {
		$model = new Pdd();
		$res = $model->goodsSearch();
	
//		$res = $model->goodsDetail('812681665');
		dump($res);
	}
	
	
	public function rebate() {
		$orderId = '1676';
		$orderAddress = Db::name('orderAddress')->where(['order_id' => $orderId])->find();
		$agentAreaModel = new AgentArea();
		$where = '(grade = 1 and province_id = '.$orderAddress['province_id'].') or (grade = 2 and city_id = '.$orderAddress['city_id'].') or (grade = 3 and area_id = '.$orderAddress['area_id'].')';
		$areaAgents = Db::name('agentArea')
			->where($where)
			->column('user_id', 'grade');
		echo Db::name('agentArea')->getLastSql();
		dump($areaAgents);
	}
	
	public function pregTmall(){
		$url = 'https://detail.m.tmall.com/item.htm?id=543540460007';
		$cacheName = 'tmall_detail_'.md5($url);
		if(!$imgs = Cache::get($cacheName)) {
			$str = file_get_contents($url);
			$detailPreg = '/<div class="module-container item_picture"(.*?)<div class="module-container custompriceDesc"/ism';
			$detailRes = preg_match($detailPreg, $str, $detailResult);
			$imgPreg = '/data-ks-lazyload="(.*?)"/';
			$imgRes = preg_match_all($imgPreg, $detailResult[0], $imgs);
			Cache::set($cacheName, $imgs[1], 300);
			$imgs = $imgs[1];
		}
		
		foreach ($imgs as $img) {
			echo '<img style="width:100%;" src="'.$img.'">';
		}
	}
	
	
	public function pregTaobao(){
		$id = $this->request->request('id');
		$url = 'https://detail.m.tmall.com/item.htm?id='.$id;
		$cacheName = 'tmall_detail_'.md5($url);
		$cacheStrName = 'tmall_detail_str_'.md5($url);
		
		if(!$data = Cache::get($cacheName)) {
			if(!$str = Cache::get($cacheStrName)) {
				$str = file_get_contents($url);
				Cache::set($cacheStrName, $str, 300);
			}
			
			$sliderPreg = '/<section class="mui-slider preview-slider">(.*?)<\/section>/';
			$sliderRes = preg_match($sliderPreg, $str, $sliderResult);
			
			$sliderImgPreg = '/data-src="(.*?)"/';
			$imgRes = preg_match_all($sliderImgPreg, $sliderResult[0], $sliderImgs);
			$sliderImgs = $sliderImgs[1];
			
			$prefixPreg = '/^http|https/';
			$banner = [];
			foreach ($sliderImgs as $img) {
				if(!preg_match($prefixPreg, $img)) {
					$img = 'https:'.$img;
				}
				$banner[] = $img;
			}
			
			$detailPreg = '/<div class="module-container(.*?)<section class="actionBar-bg">/ism';
			$detailRes = preg_match($detailPreg, $str, $detailResult);
			$imgPreg = '/data-ks-lazyload="(.*?)"/';
			$imgRes = preg_match_all($imgPreg, $detailResult[0], $imgs);
			$imgs = $imgs[1];
			$detailImgs = [];
			foreach ($imgs as $img) {
				if(!preg_match($prefixPreg, $img)) {
					$img = 'https:'.$img;
				}
				$detailImgs[] = $img;
			}
			
			$data = [
				'banners' => $banner,
				'detailImgs' => $detailImgs
			];
			
			Cache::set($cacheName, $data, 500);
		}
		
			
		
		dump($data);
	}
}